package com.system.healthproject.utils;


import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SimpleFormatutiles {
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
    public static final String HOUR_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String MONTH_PATTERN = "yyyy-MM";
    public static final String YEAR_PATTERN = "yyyy";
    public static final String MINUTE_ONLY_PATTERN = "MM";
    public static final String HOUR_ONLY_PATTERN = "dd";
    /**
	 * 验证参数是否为空 为空
	 * @param obj
	 * @return
	 */
    public static String  isNull(Object  obj,String str){
		if(obj==null){
			return  "";
		}
		if(obj instanceof  Date){
			return  new SimpleDateFormat(str).format(obj);
		}
		return  obj.toString();
	}

    /**
     * 时间转换成字符串
     * @param date  时间
     * @param formatStr 转化的格式
     * @return
     */
    public static String dateString(Date date, String formatStr) {
        String strDate = "";
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        strDate = sdf.format(date);
        return strDate;
    }

    /**
     * 格式化日期对象
     * @param date
     * @param formatStr 转化的格式
     * @return
     */
    public static Date dataParse(Date date, String formatStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        String str = sdf.format(date);
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            return null;
        }
        return date;
    }

    /**
     * 字符串转时间
     * @param dateString    字符串
     * @param formatStr 转化的格式
     * @return
     */
    public static Date stringdate(String dateString, String formatStr) {
        Date formateDate = null;
        DateFormat format = new SimpleDateFormat(formatStr);
        try {
            formateDate = format.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
        return formateDate;
    }

    /**
     * date类型转换成Timestamp类型
     * @param date
     * @return
     */
    public static Timestamp datetimestamp(Date date) {
        if (date == null)
            return null;
        return new Timestamp(date.getTime());
    }


    /**
     * @return
     * @描述 —— 获得当前年份
     */
    public static String getNowYear() {
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_PATTERN);
        return sdf.format(new Date());
    }

    /**
     * @return
     * @描述 —— 获得当前月份
     */
    public static String getNowMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat(MINUTE_ONLY_PATTERN);
        return sdf.format(new Date());
    }

    /**
     * @return
     * @描述 —— 获得当前日期中的日
     */
    public static String getNowDay(){
        SimpleDateFormat sdf = new SimpleDateFormat(HOUR_ONLY_PATTERN);
        return sdf.format(new Date());
    }

    /**
     * 时间加小时
     * @param startDate
     * @param hours
     * @return
     */
    public static Date AddHours(Date startDate, int hours) {
        if (startDate == null) {
            startDate = new Date();
        }
       Calendar c = Calendar.getInstance();
       c.setTime(startDate);
       c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hours);
       return c.getTime();
       }

    /**
     * 时间加分钟
     * @param startDate
     * @param minutes
     * @return
     */
    public static Date AddMinutes(Date startDate, int minutes) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes);
        return c.getTime();
    }

    /**
     * 时间加秒
     * @param startDate
     * @param seconds
     * @return
     */
    public static Date AddSeconds(Date startDate, int seconds) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.SECOND, c.get(Calendar.SECOND) + seconds);
        return c.getTime();
    }

    /**
     * 增加天数
     * @param startDate
     * @param day
     * @return
     */
    public static Date AddDay(Date startDate, int day) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.DATE, c.get(Calendar.DATE) + day);
        return c.getTime();
    }

    /**
     * 增加月份
     * @param startDate
     * @param months
     * @return
     */
    public static Date AddMonths(Date startDate, int months) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) + months);
        return c.getTime();
    }

    /**
     * 增加年份
     * @param startDate
     * @param years
     * @return
     */
    public static Date AddYears(Date startDate, int years) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.YEAR, c.get(Calendar.YEAR) + years);
        return c.getTime();
    }
    public static int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }
        return age;
    }

}
